package com.cisdi.blog.web;

import com.cisdi.blog.entity.*;
import com.cisdi.blog.service.*;
import com.cisdi.blog.vo.BlogQuery;
import com.cisdi.blog.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    BlogService blogService;
    @Autowired
    TypeService typeService;
    @Autowired
    TagService tagService;
    @Autowired
    CommentService commentService;
    @Autowired
    UserService userService;

    @GetMapping("/")
    public String blogs(@PageableDefault(size = 10,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery query, Model model){

        model.addAttribute("page",blogService.listBlog(pageable,query));
        model.addAttribute("types",typeService.hotType(6));
        model.addAttribute("tags",tagService.hotTag(6));
        model.addAttribute("blogs",blogService.hotBlog(6));
        model.addAttribute("user",userService.getUser());
        return "index";
    }

    @GetMapping("/blog/{id}")
    public String showBlog(@PathVariable("id") Long id, Model model){
        Blog blog = blogService.show(id);
        model.addAttribute("blog",blog);
        model.addAttribute("comments",commentService.getNodeComment(blog.getComments()));
        return "blog";
    }

    @GetMapping("/tags/{id}")
    public String tags(@PathVariable Long id,@PageableDefault(sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, Model model){
        List<Tag> tags = tagService.listTag();
        tags.sort((o1, o2) -> o2.getBlogs().size()-o1.getBlogs().size());
        if (id==-1){
            id = tags.get(0).getId();
        }
        Page page = blogService.listBlog(id, pageable);
        model.addAttribute("tags",tags);
        model.addAttribute("page",page);
        model.addAttribute("selectId",id);
        return "tags";
    }

    @GetMapping("/types/{id}")
    public String types(@PathVariable("id") Long id,@PageableDefault(sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,Model model){
        List<Type> types = typeService.hotType(1000);
        types.sort((o1, o2) -> o2.getBlogs().size()-o1.getBlogs().size());
        model.addAttribute("types",types);
        if (id==-1){
            id = types.get(0).getId();
        }
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setTypeId(id);
        Page page = blogService.listBlog(pageable, blogQuery);
        model.addAttribute("page",page);
        model.addAttribute("selectId",id);
        return "types";
    }

    @GetMapping("/archive")
    public String archive(Model model){
        List<Map<String, Object>> list = blogService.getArchiveMap();
        model.addAttribute("list",list);
        model.addAttribute("total",blogService.getBlogTotal());
        return "archives";
    }

    @GetMapping("/about")
    public String about(Model model){
        model.addAttribute("user",userService.getUser());
        return "about";
    }

    @GetMapping("/footerInfo")
    public String footerInfo(Model model){
        User user = userService.getUser();
        model.addAttribute("newblog",blogService.hotBlog(3));
        model.addAttribute("avatar",user.getAvatar());
        model.addAttribute("email",user.getEmail());
        return "_fragment::footerInfo";
    }

    @GetMapping("/search")
    public String search(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         @RequestParam String query, Model model){
        BlogQuery blogQuery = new BlogQuery();
        blogQuery.setSearch(query);
        Page page = blogService.listBlog(pageable, query);
        model.addAttribute("page",page);
        model.addAttribute("query",query);
        return "search";
    }



}
